<?php
namespace app\index\controller;

use app\index\model\AuthRule;
use think\auth\Auth;
use think\Facade\Cache;
use think\Controller;

class Base extends Controller
{
    public function initialize()
    {
        parent::initialize();

        $controller = request()->controller();
        $action = request()->action();

        $url = $controller.'/'.$action;
        $noCheck = ['Index/login', 'Index/logout'];

        $uid = session('uid');
        $auth = new Auth();
        if ($uid != 1) {
            if (!in_array($url, $noCheck)) {
                if (!$uid) {
                    $this->error('请先登陆系统！',url('index/login'));
                }
                if (!$auth->check($url, $uid)) {
                    $this->error('没有权限', 'index/login');
                }
            }

            //获取
            //$group = $auth->getGroups($uid);
        }else{
            $menus_data = Cache::get('menus');
            if (!$menus_data) {
                //获取所有的菜单
                $auth_rule = new AuthRule();
                $menus = $auth_rule->getList();
                $menus_data = getTree1($menus);
                Cache::set('menus',  $menus_data, 0);
            }

            $this->assign('menus', $menus_data);
        }
        $this->assign('url', $url);

    }
}
